package com.marvels.product.param.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.ProductItemService;
import com.marvels.product.productmanage.dto.McptProductTypeDto;
/**
 * 产品小类
 * @author 宁华勇
 *
 */
@RestController
@RequestMapping("/product/param/product/service")
public class ProductItemServiceController {
	
	@Autowired
	private ProductItemService itemService;
	
	
	/**
	 * 基于版本查询产品
	 * @param param
	 * @return
	 */
	@RequestMapping("vlist")
	public MarvelsResultDTO getListByVersion(@RequestBody Map<String,Object> param){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(param));
		
		Map<String, Object> dataMap = null;
		
		PageInfo<McptProduct> pageInfo = null;
		 try {
			pageInfo = itemService.getPageInfoByVersion(param);
			dataMap = itemService.initDataMap(new McptProductTypeDto());
			dataMap.put("pageInfo", pageInfo);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dataMap);
	}
	
	
	/**
	 * 更新审核状态   ---审核中   使用  停用  拒绝
	 * @param dto
	 * @return
	 */
	@RequestMapping("updateStatus")
	public MarvelsResultDTO updateStatus(@RequestBody McptProduct dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			itemService.updateStatus(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	/**
	 * 删除产品
	 * @param productId
	 * @return
	 */
	@RequestMapping("delete")
	public MarvelsResultDTO delete(@PathVariable Long productId){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(productId));
		try {
			itemService.deleteByPrimaryKey(productId);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	@RequestMapping("getInitInfo")
	@ResponseBody
	public MarvelsResultDTO getInitInfo(@RequestBody McptProductTypeDto productTypeDto){
		Map<String, Object> dataMap = null;
		 try {
			dataMap = itemService.initDataMap(productTypeDto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dataMap);
	}
	
	/*
	 * 批量编辑
	 */
	@RequestMapping("batchUpdate")
	public MarvelsResultDTO batchUpdate(@RequestBody List<McptProduct> dtoList){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dtoList));
		try {
			itemService.updateBatchItem(dtoList);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	@RequestMapping("save")
	public MarvelsResultDTO save(@RequestBody McptProduct dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setCreatedBy(userName);
			itemService.save(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	
}
